﻿using RestSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Ink;
using static AutoTestCommunication_Service.Public;

namespace AutoTestCommunication_Service.Class
{
    public class IRAYClass
    {
         static int Xrate = 12820;  static int Yrate = 16129;
        
        public static bool GetToken(string IP, out string Token)
        {
            try
            {
                string strtoken = "";

                var client = new RestClient("http://"+IP+"/v1/user/login?date=1587022819092&username=admin&password=admin");
                var request = new RestRequest(Method.GET);
                //request.AddHeader("HEADER", "1");
                IRestResponse response = client.Execute(request);
                //Console.WriteLine(response.Content);
                string factMessage = response.Content;
                string[] yunArray = factMessage.Split(new string[] { ",", ":" }, StringSplitOptions.RemoveEmptyEntries);
                char[] charsToTrim = { '}', ' ', '\'','"'};
                strtoken = yunArray[11].Trim(charsToTrim);
                Token = strtoken;
                return true;
            }
            catch (Exception ex)
            {
                string strtoken = "";
                Token = strtoken;
                 TraceMethodInfo(ex.Message+ex.StackTrace);
                //PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = "";
                return false;
            }

        }
     
        public static bool DelTemperatureArea(string IP,string Token,int index)
        {
            try
            {
                var client = new RestClient("http://"+IP+"/v1/cmd07/areathermometry/DelTemperatureArea?index="+index+"&mode=2");
                var request = new RestRequest(Method.PUT);
                request.AddHeader("X-Token", Token);
                IRestResponse response = client.Execute(request);
                return true;
            }
            catch (Exception ex)
            {
                 TraceMethodInfo(ex.Message+ex.StackTrace);
                return false;
            }

        }

        public static bool AddTemperatureArea(string IP, string Token,int startX,int startY,int endX,int endY)
        {
            try
            {
                startX = (startX * Xrate) / 1000;
                startY = (startY * Yrate) / 1000;
                endX = (endX * Xrate) / 1000;
                endY = (endY * Yrate) / 1000;
                var client = new RestClient("http://"+IP+"/v1/cmd07/areathermometry/AddTemperatureArea?mode=2&startX="+startX+"&startY="+startY+"&endX="+endX+"&endY="+endY+"");
                var request = new RestRequest(Method.PUT);
                request.AddHeader("X-Token", Token);
                IRestResponse response = client.Execute(request);
                //Console.WriteLine(response.Content);

                return true;
            }
            catch (Exception ex)
            {
                 TraceMethodInfo(ex.Message+ex.StackTrace);
                return false;
            }

        }
    }
}
